/*!
 *	CODESPACE ++
 *	A real-time syntax highlighting javascript editor
 *
 *	Copyright (C) 2008 Francois Botman, <Franchie.brightspark@gmail.com>
 *
 *	This script is licenced under: Creative Commons Attribution-Noncommercial-Share Alike 3.0
 *	(see http://creativecommons.org/licenses/by-nc-sa/3.0/).
 *	It bascially means that it can be modified and shared, provided it is
 *	clearly attributed, and can be used in personnal/charity projects for free!
 *	(Commercial use requires prior negotiation with the author).
 *	In any case, should anything go wrong, I cannot be held responsible!
 *
 *	Based on an original idea by Fernando M.A.d.S.,  http://codepress.org/
 */


/*
 * This is the editor main componant.
 * It must be invoked in the following way:
 *
 * [handle =] new Codespace(container [,options]);
 * Where container is either an element into which the editor should be put, or a textarea that the editor should replace
 *
 * Possible options (all are optionnal) :
 *	- text: initial text that the editor contains. It defaults to the textare's contents if there is any.
 *	- style: a hash of style attributes that the new editor will try to have.
 *
 * This function always succeeds, but may create a "compatibility" version instead.
 */
function Codespace(container, options)
{
	// Ok, some private variables to start off with!
	var self = this;
	var opt = {
		text: 			null,		// This can be used to specify the initial text...
		style: 			{},			// This can be used to specify styles...
		language:		'default',	// What language (for highlighting)
		
		readOnly: 		false,		// Set to true to have a read-only editor
		showLineNumbers:true,		// Show/hide line numbers
		showFolding: 	true,		// Show/hide the code folding bar
		
		lineNumberBar: 	0,			// Order of the table bars
		foldingBar:		1,
		contentBar:		2
	};
	for(var i in options) opt[i] = options[i];
	var cont = document.getElementById(container) || container;
	
	// First, check for compatible browser. If a browser is found to be incompatible, a "compatibility" textarea is placed instead
	/*if(!isCompatible()) {
		new Codespace.CompatibleEditor(self, container);
		return self;
	}*/
	
	
	// Otherwise start creating this editor properly said...
	// If we are basing ourselves on an existing textarea, its a bit longer...
	if(cont.tagName.match(/textarea/i)) {
		// Editor will be created with the text contained in the textarea if there is no otherwise specified text
		opt.text = opt.text || cont.value;
		
		// Get all styles from  the editor (if none were already specified)
		for(var style in cont.style) if(!opt.style[style]) opt.style[style] = cont.style[style];
		
		// Next get its insertion point and hide the textarea
		var div = document.createElement('div');
		cont.parentNode.insertBefore(div, cont);
		cont.style.display = 'none';
		cont.onsubmit = function(e) {
			// If the textarea is in a form, and its value is being submitted, replace its value with the *real* value...
			alert(e+' is being submitted!');
		}
		cont = div;
	}
	
	
	// Now create a generic editor, all options are now the same in both cases.
	var table = document.createElement('table');
	cont.appendChild(table);
	
	// Next try to apply styles. Not all styles are allowed, so errors need to be tried and caught:
	table.style.padding = '0px';
	table.style.borderCollapse = 'collapse';
	
	for(var i in opt.style) {
		try {
			cont.style[i] = opt.style[i];
		} catch(e) {}
	}
	cont.style.overflow = 'auto';
	table.style.cellCollapse = 'collapse';
	table.style.cellSpacing  = '0px';
	table.style.MozUserSelect = 'none';
	table.style.KhtmlUserSelect = 'none';
	table.style.userSelect = 'none';
	table.style.textAlign = 'left';
	table.className = "codespace unfocused";
	table.unselectable = 'on';
	table.ondrag = function () { return false; };
	table.onselectstart = function () { return false; }
	
	// And now actually create the individual componants
	opt.style = null;
	var editor = new Codespace.Editor(self, table, cont, opt);
}


// debug
function dBug(a) {window.status = a.toString();}
if(!window.console) window.console={log: dBug, error: dBug};








